package com.onlineMIS.ORM.DAO.headQ.finance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.headQ.finance.ExpenseHqType;
import com.onlineMIS.ORM.entity.headQ.supplier.finance.FinanceBillSupplierItem;
import com.onlineMIS.common.Common_util;


@Repository
public class ExpenseHqTypeImpl extends BaseDAO<ExpenseHqType>{

	/**
	 * 根据费用名称查询费用
	 * @param name
	 * @return
	 */
	public ExpenseHqType selectByExpenseName(String name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHqType.class);
		criteria.add(Restrictions.eq("name",name));
		List<ExpenseHqType> types =  getByCritera(criteria, true);
		if (types != null  && types.size() != 0) {
			return types.get(0);
		}
		return null;
	}

	public List<ExpenseHqType> selectByType(int type) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHqType.class);
		criteria.add(Restrictions.eq("type",type));
		return getByCritera(criteria, true);
		
	}
	
	
	/**
	 * 根据费用归属获取费用类型（店铺用）
	 * @param belong
	 * @return
	 */
	public List<ExpenseHqType> getExpenseHqTypes(Integer belong){
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHqType.class);	
        criteria.add(Restrictions.eq("belong", belong));
        criteria.add(Restrictions.eq("status", ExpenseHqType.STATUS_NORMAL));
        criteria.add(Restrictions.isNotNull("parentId"));
		List<ExpenseHqType> resultList = this.getByCritera(criteria, true); 
		
		return resultList;
	}

	/**
	 * 费用类型下拉框
	 * @return
	 */
	public List<ExpenseHqType> selectTypes() {
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHqType.class);		
		criteria.add(Restrictions.eq("status", ExpenseHqType.STATUS_NORMAL));
		criteria.addOrder(Order.asc("sort"));
		return  getByCritera(criteria, true);

	}

	

	/**
	 * 获取总部费用
	 * @param belong
	 * @param level
	 * @return
	 */
	public List<ExpenseHqType> selectExpenseHq() {
		List<ExpenseHqType> listVO = new ArrayList<ExpenseHqType>();
		List<ExpenseHqType> list = new ArrayList<ExpenseHqType>();
		List<Integer> ids = new ArrayList<Integer>();
		String sql = "select id from ExpenseHqType where type="+ExpenseHqType.COST_TYPE_HQ+ " and id NOT IN( select parentId from ExpenseHqType where parentId is not null) ";

		List<Object> results = this.executeHQLSelect(sql, new Object[] {}, null, true);

		if (results != null) {
			for (Object object : results) {
				ids.add((int) object);
			}
		}		
		if (ids !=null) {
			list = selectExpenseTypes(ids);
		}
		if (list.size() != 0) {
			for (ExpenseHqType type : list) {
				ExpenseHqType ex = this.get(type.getParentId(), true);
				
				ExpenseHqType exvo = new ExpenseHqType();
				BeanUtils.copyProperties(type, exvo);
				exvo.setName(ex.getName() + ":" + type.getName());
				
				listVO.add(exvo);
			}
		}				
		return listVO;
	}
	
	
	/**
	 * 根据归属和级别获取费用集合
	 * @param expenseLevelThree
	 * @return
	 */
	public List<ExpenseHqType> selectExpenseTypes(List<Integer> ids ) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHqType.class);
		criteria.add(Restrictions.in("id", ids));
		criteria.add(Restrictions.eq("status", ExpenseHqType.STATUS_NORMAL));
		return  getByCritera(criteria, true);
		
	}
	
	
	
	/**
	 * 获取总部收益费用集合
	 * @return
	 */
	public List<ExpenseHqType> selectExpenseHqIncome() {
		List<ExpenseHqType> listVO = new ArrayList<ExpenseHqType>();
		List<ExpenseHqType> list = new ArrayList<ExpenseHqType>();
		List<Integer> ids = new ArrayList<Integer>();
		String sql = "select id from ExpenseHqType where type="+ExpenseHqType.COST_TYPE_HQ_INCOME+ " and id NOT IN( select parentId from ExpenseHqType where parentId is not null) ";

		List<Object> results = this.executeHQLSelect(sql, new Object[] {}, null, true);

		if (results != null) {
			for (Object object : results) {
				ids.add((int) object);
			}
		}		
		if (ids !=null) {
			list = selectExpenseTypes(ids);
		}
		if (list.size() != 0) {
			for (ExpenseHqType type : list) {
				ExpenseHqType ex = this.get(type.getParentId(), true);
				
				ExpenseHqType exvo = new ExpenseHqType();
				BeanUtils.copyProperties(type, exvo);
				exvo.setName(ex.getName() + ":" + type.getName());
				
				listVO.add(exvo);
			}
		}				
		return listVO;
	}

	




}
